package com.zzt.springbootdemo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-02-20
 * Time: 17:10
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session){
        //参数校验
//        if(userName == null || userName.equals("") || password == null || password.equals("")){
//            return false;
//        }
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        //校验用户名和密码是否正确
        //还未学习数据库相关的操作，暂且把账号和密码写死 admin admin
        if("admin".equals(userName) && "admin".equals(password)){
            session.setAttribute("userName", userName);
            return true;
        }
        return false;
    }


    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session){
        String userName = (String)session.getAttribute("userName");
        return userName;
    }


    public Boolean login2(String userName, String password){
        if(StringUtils.hasLength(userName) && StringUtils.hasLength(password)){
            if("admin".equals(userName) && "admin".equals(password)){
                return true;
            }
        }
        return false;
    }
}



